#include "ElementDTD.h"
#include <string.h>
#include "ComposedStructDTD.h"
#include <iostream>

using namespace std;

ElementDTD::ElementDTD(const char * _name, StructureDTD * _s) : name(_name), s(_s)
{
}


int ElementDTD::addAttribute(AttributeDTD* att)
{
    //atts.push_back(att);

    pair<map<string,AttributeDTD*>::iterator, bool> ret;

    ret = atts.insert( pair<string, AttributeDTD*>(att->getName(), att) );

    if(ret.second == false)
    {
        printf("Attribute \"%s\" already exists for element \"%s\"\n", att->getName().c_str(), this->name.c_str());
        return -1;
    }

    else
        return 0;
}

string ElementDTD::getName() const
{
    return name;
}


AttributeDTD* ElementDTD::getAttributeByName(string name) const
{
    map<string,AttributeDTD*>::const_iterator it;

    it = atts.find(name);

    if ( it == atts.end() )
    {
        return NULL; // the element named name doesn't exist
    }

    return it->second;
}

vector<string> * ElementDTD::getChildren() const
{
    ComposedStructDTD *cs = static_cast<ComposedStructDTD*> (s);
    if ( cs == NULL ) /* if the structure of the element is not a composed structure */
    {
        return NULL;
    }
    return cs->getChildren();

}

string ElementDTD::getRegExp() const
{
    return s->regExpCreate();
}

void ElementDTD::display() const
{
    printf("\tELEMENT : %s\n", name.c_str() );
    printf("\tAttributs : ");

    /* displaying all attributes */
    map<string,AttributeDTD*>::const_iterator it;

    for ( it=atts.begin() ; it != atts.end(); it++ )
    {
        ((*it).second)->display();
        printf(" ");
    }
    printf("\n");

    printf("\tStructure : ");
    s->display();
}
